package com.ruoyi.web.controller.party;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.party.domain.PartyCategory;
import com.ruoyi.party.service.IPartyCategoryService;
import com.ruoyi.party.service.IPartyPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 客户文章分类Controller
 * 
 * @author myt
 * @date 2024-07-07
 */
@RestController
@RequestMapping("/party/category")
public class PartyCategoryController extends BaseController
{
    @Autowired
    private IPartyCategoryService partyCategoryService;

    @Autowired
    private IPartyPersonService partyPersonService;

    /**
     * 查询客户文章分类列表
     */
    @Anonymous
//    @PreAuthorize("@ss.hasPermi('party:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(PartyCategory partyCategory)
    {
        startPage();
        List<PartyCategory> list = partyCategoryService.selectPartyCategoryList(partyCategory);
        return getDataTable(list);
    }

    /**
     * 导出客户文章分类列表
     */
    @PreAuthorize("@ss.hasPermi('party:category:export')")
    @Log(title = "客户文章分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartyCategory partyCategory)
    {
        List<PartyCategory> list = partyCategoryService.selectPartyCategoryList(partyCategory);
        ExcelUtil<PartyCategory> util = new ExcelUtil<PartyCategory>(PartyCategory.class);
        util.exportExcel(response, list, "客户文章分类数据");
    }

    /**
     * 获取客户文章分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('party:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(partyCategoryService.selectPartyCategoryById(id));
    }

    /**
     * 新增客户文章分类
     */
    @PreAuthorize("@ss.hasPermi('party:category:add')")
    @Log(title = "客户文章分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PartyCategory partyCategory)
    {
        return toAjax(partyCategoryService.insertPartyCategory(partyCategory));
    }

    /**
     * 修改客户文章分类
     */
    @PreAuthorize("@ss.hasPermi('party:category:edit')")
    @Log(title = "客户文章分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PartyCategory partyCategory)
    {
        return toAjax(partyCategoryService.updatePartyCategory(partyCategory));
    }

    /**
     * 删除客户文章分类
     */
    @PreAuthorize("@ss.hasPermi('party:category:remove')")
    @Log(title = "客户文章分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return toAjax(partyCategoryService.deletePartyCategoryByIds(ids));
    }

    /**
     * 删除客户文章分类
     */
    @PreAuthorize("@ss.hasPermi('party:category:remove')")
    @Log(title = "客户文章分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public AjaxResult removes(@PathVariable Long id)
    {
        if (partyPersonService.isCategoryIdExists(id))
        {
            return warn("删除分类'" + id + "'失败，分类下面绑有产品");
        } else {
            return toAjax(partyCategoryService.deletePartyCategoryById(id));
        }

    }
}
